package com.xxx.jsonrpc;

import java.net.MalformedURLException;
import java.net.URL;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.xxx.jsonrpc.DemoBean;

/**
 * JsonRpc demo
 */
public class Test {
	
	static JsonRpcHttpClient jsonRpcClient; // jsonRpcClient
	
	/**
	 * 初始化JsonRpcClient
	 */
	static {
		try {
			jsonRpcClient = new JsonRpcHttpClient(new URL("http://127.0.0.1:8080/rpc"));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Throwable {
		try {
			jsonRpcClient.invoke("doSomething", null);
			
			Test test = new Test();
			
			DemoBean demo = test.getDemo(1, "abc");
			System.out.println("===test javabean, demo:"+demo.toString());
			
			int code = test.getInteger(2);
			System.out.println("===test Integer, code:" + code);
			
			String msg = test.getString("呵呵123");
			System.out.println("===test String, msg:" + msg);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DemoBean getDemo(int code, String msg) throws Throwable {
		String[] params = new String[] { String.valueOf(code), msg };
		return jsonRpcClient.invoke("getDemo", params, DemoBean.class);
	}

	public int getInteger(int code) throws Throwable {
		Integer[] codes = new Integer[] { code };
		return jsonRpcClient.invoke("getInteger", codes, Integer.class);
	}

	public String getString(String msg) throws Throwable {
		String[] msgs = new String[] { msg };
		return jsonRpcClient.invoke("getString", msgs, String.class);
	}

}
